<?php
	/* **********************************************************/
	/* Arcans Project (2010)                                    */
	/* Jeu sous forme d'application web entièrement conçu       */
	/* et réalisé par Killian Le Maitre                         */
	/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
	/* **********************************************************/

	//*******************************
	//classe de gestion des pages xml
	//*******************************
	class ObjXmlClass {
		//propriétés :
		private $title;
		private $menu;
		private $root;
		private $infos;
		private $rootinfos;
		private $rootext;
		private $structure;
		//méthodes :
		//constructeur ********************************************************************************
			//constructeur site.xml
		function __construct($level_g, $address) {
			$site_xml = new DomDocument();
			$site_xml->load("xml/site.xml");
			$listpage = $site_xml->getElementsByTagName("page");
			$i = 0;
			foreach ($listpage as $elt) {
				$type = $elt->getAttribute("type");
				$level = $elt->getAttribute("level");
				if ($level_g) {
					if ($level >= 1 || $level <= -1) {
						if ($type == $address) {
							$path = $elt->getAttribute("path");
							$this->title = $elt->getAttribute("name");
						}
						elseif ($level > -1) $this->menu[$i] = $elt; $i++;
					}
				}
				else {
					if ($level <= 1 && $level >= -1) {
						if ($type == $address) {
							$path = $elt->getAttribute("path");
							$this->title = $elt->getAttribute("name");
						}
						elseif ($level > -1) $this->menu[$i] = $elt; $i++;
					}
				}
			}
			if ($path == NULL) $path = "404.xml";
			$this->dom = new DomDocument();
			$this->dom->load("xml/".$path);
			$this->root = $this->dom->documentElement;
			$this->rootinfos = $this->dom->getElementsByTagName("info")->item(0);
			$this->rootext = $this->dom->getElementsByTagName("page")->item(0);
			$this->structure = $this->createStruct();
			$this->createInfo();
		}
		//méthodes publiques : ************************************************************************
		public function pageTitle() { return $this->title; }
		public function infoHead() { return $this->infos; }
		public function structText() { return $this->structure; }
		public function infoMenu() { return $this->menu; }
		//méthodes privées : **************************************************************************
		private function createStruct() {
			if ($this->rootext->hasChildNodes()) $page_struct = $this->switchStruct($this->rootext);
			else {
				$page_struct[0]["type"] = "text";
				$page_struct[0]["data"] = "La page que vous demandez est en cours de construction.";
			}
			return $page_struct;
		}
		private function createInfo() {
			if ($this->rootinfos->hasChildNodes()) {
				$i = 0;
				foreach ($this->rootinfos->childNodes as $elt) {
					if ($elt->nodeName == "elt") {
						$this->infos[$i]["type"] = $elt->getAttribute("type");
						$this->infos[$i]["path"] = $elt->getAttribute("path");
						$i++;
					}
				}
			}
			else $this->infos[0]["type"] = "noinfos";
		}
		private function switchStruct($node) {
			$i = 0;
			foreach ($node->childNodes as $elt) {
				if ($elt->nodeName == "elt") {
					$node_struct[$i]["type"] = $elt->getAttribute("type");
					switch ($elt->getAttribute("type")) {
						case "table" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("id");
							$node_struct[$i]["data"][1] = $this->switchStruct($elt); break;
						case "contain" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("id");
							$node_struct[$i]["data"][1] = $this->switchStruct($elt); break;
						case "row" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("id");
							$node_struct[$i]["data"][1] = $this->switchStruct($elt); break;
						case "liste" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("style");
							$node_struct[$i]["data"][1] = $elt->getAttribute("id");
							$node_struct[$i]["data"][2] = $this->switchStruct($elt); break;
						case "textarea" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("id");
							$node_struct[$i]["data"][1] = $elt->getAttribute("onclick"); break;
						case "div" : $node_struct[$i]["data"] = $elt->getAttribute("id"); break;
						case "cellf" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("cols");
							$node_struct[$i]["data"][1] = $elt->getAttribute("id");
							$node_struct[$i]["data"][2] = $this->switchStruct($elt); break;
						case "label" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("for");
							$node_struct[$i]["data"][1] = $elt->nodeValue; break;
						case "input" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("version");
							$node_struct[$i]["data"][1] = $elt->getAttribute("id");
							if ($node_struct[$i]["data"][0] == "hidden") {
								$mysqlrec = new MysqlClass(0);
								$res = $mysqlrec->resultFun("select", $elt->getAttribute("table"), "users='".$_SESSION["pseudo"]."'");
								$node_struct[$i]["data"][2] = $res[$elt->nodeValue];
							}
							else {
								$node_struct[$i]["data"][2] = $elt->nodeValue;
								$node_struct[$i]["data"][3] = $elt->getAttribute("onclick");
							}
							break;
						case "button" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("id");
							$node_struct[$i]["data"][1] = $elt->getAttribute("onclick");
							$node_struct[$i]["data"][2] = $elt->nodeValue; break;
						case "link" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("href");
							$node_struct[$i]["data"][1] = $elt->nodeValue; break;
						case "champs" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("table");
							$node_struct[$i]["data"][1] = $elt->nodeValue; break;
						case "connect" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("id");
							$node_struct[$i]["data"][1] = $this->switchStruct($elt); break;
						case "eltlst" :
							$node_struct[$i]["data"][0] = $elt->getAttribute("style");
							$node_struct[$i]["data"][1] = $elt->nodeValue; break;
						default : $node_struct[$i]["data"] = $elt->nodeValue; break;
					}
					$i++;
				}
			}
			return $node_struct;
		}
	}
?>